package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.BrandMapper;
import cn.cmdr.mapper.ComMapper;
import cn.cmdr.pojo.Brand;
import cn.cmdr.pojo.Commodity;
import cn.cmdr.service.BrandService;
import cn.cmdr.service.ComService;
import cn.cmdr.vo.BrandVo;
import cn.cmdr.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private ComService comService;
    @Override
    public PageVo getPageBrandList(Integer pageNo, Integer pageSize, BrandVo brandVo) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(!StringUtils.isEmpty(brandVo.getName()),"name",brandVo.getName())
                .orderByDesc("sort");
        IPage<Brand> iPage = super.page(page,queryWrapper);
        for (Brand b :iPage.getRecords()){
            b.setProductCount(comService.getcBYyId(b.getId()).size());

        }
        return new PageVo(iPage.getRecords(),iPage.getTotal());
    }

    @Override
    public void addBrand(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateById(brand);
    }

    @Override
    public void del(Integer[] id) {
//        brandMapper.deleteById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id",id);
        brandMapper.delete(queryWrapper);
    }

    @Override
    public List<Brand> getlist() {
        return brandMapper.selectList(null);
    }

    @Override
    public Brand getBrand(Integer bid) {
        return brandMapper.selectById(bid);
    }
}
